package system

import (
	"database/sql/driver"
	"encoding/json"

	"go-api-template/api/models"
)

type EventId int

const (
	EventLogin  EventId = iota + 1 // 登录
	EventLogout                    // 登出
	EventVisit                     // 访问
	EventClick                     // 点击
	EventDelete                    // 删除
)

type UserTrack struct {
	models.BaseModel
	UserId     string              `gorm:"column:user_id;index"` // 用户ID
	Client     string              `gorm:"column:client"`        // 客户端
	SessionId  string              `gorm:"column:session_id"`    // 会话ID
	EventId    EventId             `gorm:"column:event_id"`      // 事件ID
	Properties UserTrackProperties `gorm:"column:properties"`    // 属性
}

func (u UserTrack) TableName() string {
	return "system_user_track"
}

type UserTrackProperties map[string]interface{}

func (u *UserTrackProperties) Value() (driver.Value, error) {
	return json.Marshal(u)
}

func (u *UserTrackProperties) Scan(value interface{}) error {
	return json.Unmarshal(value.([]byte), u)
}
